package com.mryan.auth.dao;

import com.mryan.auth.pojo.Auth;
import org.apache.ibatis.annotations.*;

/**
 * @description： AuthDao
 * @Author MRyan
 * @Date 2021/6/14 17:06
 * @Version 1.0
 */
@Mapper
public interface AuthDao {

    @Insert("INSERT INTO auth (qr_code, auth_ip, auth_address)" +
            "VALUES (#{qrCode}, #{authIp}, #{authAddress})")
    Integer addAuthInfo(String qrCode, String authIp, String authAddress);

    @Select("SELECT * FROM auth WHERE auth_token = #{authToken}")
    @Results({
            @Result(property = "authToken", column = "auth_token"),
            @Result(property = "temporaryToken", column = "temporary_token"),
            @Result(property = "authTime", column = "auth_time"),
            @Result(property = "qrCode", column = "qr_code"),
            @Result(property = "deviceInfo", column = "device_info"),
            @Result(property = "authIp", column = "auth_ip"),
            @Result(property = "authAddress", column = "auth_address"),
            @Result(property = "authState", column = "auth_state"),
            @Result(property = "accountId", column = "account_id"),

    })
    Auth getAuthInfoByToken(String authToken);


    @Select("SELECT * FROM auth WHERE qr_Code = #{qrCodeId}")
    @Results({
            @Result(property = "authToken", column = "auth_token"),
            @Result(property = "temporaryToken", column = "temporary_token"),
            @Result(property = "authTime", column = "auth_time"),
            @Result(property = "qrCode", column = "qr_code"),
            @Result(property = "deviceInfo", column = "device_info"),
            @Result(property = "authIp", column = "auth_ip"),
            @Result(property = "authAddress", column = "auth_address"),
            @Result(property = "authState", column = "auth_state"),
            @Result(property = "accountId", column = "account_id"),

    })
    Auth checkAuthInfo(String qrCodeId);

    @Update("UPDATE auth SET account_id = #{accountID} , auth_state = #{authState},temporary_token = #{temporaryToken} WHERE qr_Code = #{qrCodeId}")
    Integer setAuthState(Long accountID, String qrCodeId, Integer authState, String temporaryToken);

    @Select("SELECT * FROM auth WHERE temporary_token = #{temporaryToken} AND account_id = #{accountID} AND qr_code = #{qrCode}")
    @Results({
            @Result(property = "authToken", column = "auth_token"),
            @Result(property = "temporaryToken", column = "temporary_token"),
            @Result(property = "authTime", column = "auth_time"),
            @Result(property = "qrCode", column = "qr_code"),
            @Result(property = "deviceInfo", column = "device_info"),
            @Result(property = "authIp", column = "auth_ip"),
            @Result(property = "authAddress", column = "auth_address"),
            @Result(property = "authState", column = "auth_state"),
            @Result(property = "accountId", column = "account_id"),

    })
    Auth checkToken(String temporaryToken, Long accountID, String qrCode);

    @Update("UPDATE auth SET   auth_state = #{authState},auth_token = #{token} WHERE account_id = #{accountID}  AND qr_code = #{qrCodeId}")
    Integer addAuthToken(Long accountID, String token, Integer authState, String qrCodeId);

    @Update("UPDATE auth SET   temporary_token = #{temporaryToken} WHERE account_id = #{accountID}  AND qr_code = #{qrCodeId}")
    Integer deleteTemporaryToken(Long accountID, String temporaryToken, String qrCodeId);


    @Update("UPDATE auth SET   device_info = #{deviceInfo} WHERE qr_code = #{qrCodeId}")
    Integer addDeviceInfo(String deviceInfo, String qrCodeId);

    @Select("SELECT * FROM auth WHERE qr_code = #{qrCode}")
    @Results({
            @Result(property = "authToken", column = "auth_token"),
            @Result(property = "temporaryToken", column = "temporary_token"),
            @Result(property = "authTime", column = "auth_time"),
            @Result(property = "qrCode", column = "qr_code"),
            @Result(property = "deviceInfo", column = "device_info"),
            @Result(property = "authIp", column = "auth_ip"),
            @Result(property = "authAddress", column = "auth_address"),
            @Result(property = "authState", column = "auth_state"),
            @Result(property = "accountId", column = "account_id"),

    })
    Auth getAuthInfoByQRCodeId(String qrCodeId);
}
